function toMinutes(val) {
  return (new Date(val).getMinutes() >= 10 ? new Date(val).getMinutes() : '0' + new Date(val).getMinutes()) + ':' + (new Date(val).getSeconds() >= 10 ? new Date(val).getSeconds() : '0' + new Date(val).getSeconds())
}

var nickname;
// 电台详情
djVideo_detail()
async function djVideo_detail() {
  let djID = sessionStorage.getItem('djId')
  const res = await axios({
    url: `/dj/detail/?rid=${djID}`,
    method: 'get'
  })
  let result = res.data.data;
  nickname = result.dj.nickname
  $('.djradioDetail_top')[0].innerHTML = `
  <div class="djradioDetail_t_l fx_sa">
        <img class="djradioDetail_pic" src="${result.picUrl}?param=200y200">
    </div>
    <div class="djradioDetail_t_r">
    <div class="djradioDetail_t_r_t fx_fs">
        <div class="djradioDetail_t_r_t_l"></div>
        <div class="djradioDetail_t_r_t_r">
            ${result.name}
        </div>
    </div>
    <div class="djradioDetail_t_r_d fx_fs">
        <img src="${result.dj.avatarUrl}?param=20y20" alt="">
        <span><a href="javascript:;">${result.dj.nickname}</a></span>
    </div>
    <div class="djradioDetail_t_r_bot fx_fs">
        <div>订阅(${result.subCount})</div>
        <div style="cursor: no-drop;">播放全部</div>
        <div>分享(${result.shareCount})</div>
    </div>
    <div class="djradioDetail_t_r_desc">
        <span class="djradioDetail_category">${result.category}</span>
        <span class="djradioDetail_desc">
            ${result.desc}
        </span>
    </div>
</div>  
  `
}

// 获取电台列表
djVideo_List()
async function djVideo_List(offset = 0) {
  let djID = sessionStorage.getItem('djId')
  const res = await axios({
    url: `/dj/program?rid=${djID}&limit=40&offset=${offset}`,
    method: 'get'
  })
  let result = res.data.programs;
  $('.djradioDetail_List_left_num span').html(res.data.count); //电台期数
  $('.djradioDetail_tboby').html('')
  result.forEach((item, index) => {
    $('.djradioDetail_tboby')[0].innerHTML += `
            <tr onclick="play_video(${item.mainTrackId},{title:'${item.name}',name:'${nickname}',pic:'${item.coverUrl}'})">
                <th scope="row">${offset+index+1}</th>
                <td class="fx_fs">
                    <div class="play_icon fl"></div>
                    <span class="fl">${item.name}</span>
                </td>
                <td class="video_detail"><span class="video_time">播放${item.listenerCount}</span></td>
                <td class="video_detail"><span class="video_time">赞${item.likedCount}</span></td>
                <td class="video_detail"><span class="video_time">${new Date(item.createTime).toLocaleDateString()}</span></td>
                <td class="video_detail_at"><span>${toMinutes(item.duration)}</span></td>
            </tr> 
        `
  })
  cre_Page(res.data.count, offset / 40)
  $('.djradioDetail_tboby tr').each((index, item) => {
    $(item).click(function () {
      $('.djradioDetail_tboby tr .play_icon').removeClass('play_icon_cl')
      $('.djradioDetail_tboby tr .play_icon').eq(index).addClass('play_icon_cl')
    })
  })
}


//分页按钮
async function cre_Page(val, index) {
  $('.pagination-sm').html('')
  for (let i = 1; i <= Math.ceil(val / 40); i++) {
    $('.pagination-sm')[0].innerHTML += `<li class="page-item cp"><sapn class="page-link" href="javascript:;" onclick="changePage(${i})">${i}</sapn></li>
  `
  }
  $('.pagination-sm li').eq(index).addClass('active')
}


//分页
async function changePage(index) {
  $('.pagination-sm').children().removeClass('active');
  djVideo_List((index - 1) * 40)
  $('.pagination-sm li').eq(index - 1).addClass('active')
}
